import 'package:basic_ui/common_widgets/avatar.dart';
import 'package:basic_ui/common_widgets/my_small_widget.dart';
import 'package:basic_ui/localization/string_hardcoded.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../../../../routing/app_router.dart';
import '../controllers/profile_controller.dart';

class ProfileScreen extends ConsumerWidget {
  const ProfileScreen({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final userState = ref.watch(profileControllerProvider);

    return Scaffold(
      appBar: AppBar(
        title: Text('我的'.hardcoded),
        actions: [
          IconButton(
            icon: const Icon(Icons.settings),
            onPressed: () {
              context.pushNamed(AppRoute.settings.name);
            },
          ),
        ],
      ),
      body: userState.when(
        data: (user) {
          if (user == null) {
            return Center(child: Text("No User Data".hardcoded));
          }

          return ListView(
            children: [
              // User Profile Info
              ListTile(
                leading: const Avatar(
                    radius: 40,
                    photoUrl:
                        'https://img1.baidu.com/it/u=4127323567,270110913&fm=253&fmt=auto&app=138&f=JPEG?w=506&h=500'),
                title: Text(user.displayName ?? '用户'),
                subtitle: Text(user.email ?? '暂无邮箱'),
              ),
              const Divider(),

              ListTile(
                leading: const Icon(Icons.dark_mode),
                title: Text('外观主题'.hardcoded),
                trailing: MySmallWidgets.listTileTrailingIcon(),
                onTap: () {
                  // Handle QR code verification
                },
              ),
              const Divider(),

              // Passenger Messages
              ListTile(
                leading: const Icon(Icons.description),
                title: Text('项目文档'.hardcoded),
                trailing: MySmallWidgets.listTileTrailingIcon(),
                onTap: () {
                  // Handle passenger message
                },
              ),
              const Divider(),

              // Customer Service
              ListTile(
                leading: const Icon(Icons.support_agent),
                title: Text('人工客服'.hardcoded),
                subtitle: const Text('0731-86850000'),
                trailing: MySmallWidgets.listTileTrailingIcon(),
                onTap: () {
                  // Handle customer service contact
                },
              ),
              const Divider(),

              // Invoice
              ListTile(
                leading: const Icon(Icons.account_tree),
                title: Text('更多产品'.hardcoded),
                trailing: MySmallWidgets.listTileTrailingIcon(),
                onTap: () {
                  // Handle invoice actions
                },
              ),
              const Divider(),

              // About Us
              ListTile(
                leading: const Icon(Icons.info),
                title: Text('关于我们'.hardcoded),
                trailing: MySmallWidgets.listTileTrailingIcon(),
                onTap: () {
                  context.pushNamed(AppRoute.aboutUs.name);
                },
              ),
              const Divider(),

              // Log Out button
              ListTile(
                leading: const Icon(Icons.exit_to_app),
                title: Text('退出当前账号'.hardcoded),
                onTap: () {
                  ref.read(profileControllerProvider.notifier).logOut();
                },
              ),
            ],
          );
        },
        loading: () =>
            const Center(child: CircularProgressIndicator()), // Loading state
        error: (e, stack) => Center(child: Text('Error: $e')), // Error state
      ),
    );
  }
}
